{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "97a4dc3b-136f-4322-b255-befe1be21110",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "595e2691-8a79-448f-a080-e4ab5dadf61e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting fang.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile fang.txt \n",
    "1 2 3 4 5\n",
    "6 7 8 9 10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3690291-b0c8-4473-9ada-6fde4344e573",
   "metadata": {},
   "source": [
    "___内置方法读取数据___"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a14ff50c-cc80-440b-955f-12c75ff1d83f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.,  4.,  5.],\n",
       "       [ 6.,  7.,  8.,  9., 10.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = []\n",
    "with open(\"fang.txt\") as f:\n",
    "    for line in f:\n",
    "        fileds = line.split()\n",
    "        cur_data = [float(x) for x in fileds]\n",
    "        data.append(cur_data)\n",
    "data = np.array(data)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b352697c-1bf7-45e1-9094-e6d768d476c5",
   "metadata": {},
   "source": [
    "___numpy读取文件数据___"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cf8bef78-415d-468f-8de7-f0f9bc7a9912",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.,  4.,  5.],\n",
       "       [ 6.,  7.,  8.,  9., 10.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用numpy的函数读取文件\n",
    "data = np.loadtxt(\"fang.txt\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "45fba85a-c853-43d6-871e-64f07c52fa86",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting fang.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile fang.txt\n",
    "1,2,3,4,5\n",
    "6,7,8,9,10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "257700d5-9901-44a6-96f8-653775dba32a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.,  4.,  5.],\n",
       "       [ 6.,  7.,  8.,  9., 10.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#逗号分隔符\n",
    "data = np.loadtxt(\"fang.txt\", delimiter=\",\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "88989c2f-d02b-4dc0-a4fd-c622e6b26eac",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting fang.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile fang.txt\n",
    "x,y,z,w,a,b\n",
    "1,2,3,4,5\n",
    "6,7,8,9,10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c2006ed8-45d9-4d06-a9f2-a4ad0cb22759",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nskiprows:去掉几行\\ndelimiter:分隔符\\nusecols:指定使用哪几列\\n'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#忽略行\n",
    "data = np.loadtxt(\"fang.txt\",delimiter=\",\", skiprows=1)\n",
    "data\n",
    "\"\"\"\n",
    "skiprows:去掉几行\n",
    "delimiter:分隔符\n",
    "usecols:指定使用哪几列\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03473493-07fd-4b6c-ad11-7ba7fbecdc5f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
